//turret.txt - Simple script for turrets
//Cell 0,1 - Stuff done flag. If both 0, nothing. Otherwise If non-zero, this 
//turret wont attack. If the turret is itself attacked, will set the not-attack setting back to 0.
//Cell 2,3 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

short i,target;

body;

beginstate INIT_STATE;
	break;

beginstate DEAD_STATE;
	if ((get_memory_cell(2) != 0) || (get_memory_cell(3) != 0))
		inc_flag(get_memory_cell(2),get_memory_cell(3),1);
break;

beginstate START_STATE; 
	if (who_shot_me() >= 0) {
		if ((get_memory_cell(0) != 0) || (get_memory_cell(1) != 0)) {
			if (get_attitude(ME) == 4)
				set_attitude(ME,10);
			set_flag(get_memory_cell(0),get_memory_cell(1),0);
			}

		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

	if ((get_memory_cell(0) != 0) || (get_memory_cell(1) != 0)) {
		if (get_flag(get_memory_cell(0),get_memory_cell(1)) > 0) {
			if (get_attitude(ME) >= 10) {
				set_attitude(ME,4);
				end();
				}
			}
			else if (get_flag(get_memory_cell(0),get_memory_cell(1)) == 0) {
				if (get_attitude(ME) == 4)
					set_attitude(ME,10);
				}
		}
		
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if ((get_memory_cell(0) != 0) || (get_memory_cell(1) != 0)) {
		if (get_flag(get_memory_cell(0),get_memory_cell(1)) > 0) {
			if (get_attitude(ME) >= 10)
				set_attitude(ME,4);
			set_foe_target(ME,-1);
			set_state(START_STATE);
			}
		}
	if (target_ok() == FALSE)
		set_state(START_STATE);
	if (dist_to_char(get_target()) > 8) {
		set_foe_target(ME,-1);
		turret_heal();
		set_state(START_STATE);
		}
		
	do_attack();

	turret_heal();
break;

beginstate TALKING_STATE;
	print_str("All it really is is a big fungus. It can't communicate at all.");
break;